package com.qm.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.tomcat.jni.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;
import org.springframework.web.servlet.ModelAndView;

import com.qm.model.Classes;
import com.qm.model.Course;
import com.qm.model.ImportExcelReprotModel;
import com.qm.model.Score;
import com.qm.model.Users;
import com.qm.service.AdminCourseService;
import com.qm.service.AdminStudentService;
import com.qm.service.UserService;
@Controller
@RequestMapping("/admin")
public class AdminStudentController {
	
	@Autowired
	private UserService uservice;
	@Autowired
	private AdminStudentService aservice;
	@Autowired
	private AdminCourseService acourse;
	@ResponseBody
	/**
	 * 增加一个学生信息
	 * @param unumber
	 * @param uname
	 * @param ugender
	 * @param ugrade
	 * @return
	 */
	@RequestMapping("insertStudent")
	public int insertStudent(String unumber,String uname,String ugender,int ugrade){
		int body=0;
		Users u=new Users();
		u.setUnumber(unumber);
		u.setUname(uname);
		u.setUgender(ugender);
		u.setUgrade(ugrade);
		int a=aservice.insertStudent(u);
		if(a==1){
			body=1;
		}
		return body;
		
	}
	/**
	 * 显示adminInformation
	 * @return
	 */
	@RequestMapping("adminInformation")
	public ModelAndView showIndex(){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("/admin/Users/UsersInformation");
		mav.addObject("menu", "UsersInformation");
		return mav;
	};
	/**
	 * 通过学生ID来删除该学生
	 * @param uid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("deleteStudentByUid")
	public int deleteStudentByUid(int uid){
		int result=aservice.deleteStudentByUid(uid);
		return result;
	}
	/**
	 * 对所有学生信息进行分页处理
	 * @param page
	 * @return
	 */
	@RequestMapping("studentPaging")
	public ModelAndView studentPaging(int page){
		ModelAndView mav=new ModelAndView();
		int count=5;//每页显示5行
		int pageStart=(page-1)*5;//数据库查询的起始位置
		int studnetCount=aservice.studnetCount();//总共的条数
		int totoalPage= (int) Math.ceil(studnetCount/(double)count);//向下取整
		ArrayList<Users> pageStudent=aservice.studentPaging(pageStart, count);//传回对应的学生信息
		ArrayList<Course> courseName=acourse.selectCourseName();//选择数据库中的课程信息
		mav.addObject("courseName", courseName);
		mav.addObject("student",pageStudent);
		mav.addObject("page",page);
		mav.addObject("totoalPage",totoalPage);
		mav.addObject("menu", "StudentInformation");//当前的menu名称
		mav.setViewName("/admin/Users/StudentInformation");
		return mav;
	}
	/**
	 * 
	 * @param cocourseCode
	 * @param cid
	 * @param page
	 * @return
	 */
	@RequestMapping("studentInfoByCourse")
	public ModelAndView selectStudentByCourse(int cocourseCode,int cid,int page){
		ModelAndView mav=new ModelAndView();
		int count=5;
		int pageStart=(page-1)*5;
		int studentCount=aservice.selectStudentCountByCourse(cocourseCode,cid);//当前选择条件的总人数'
		int totoalPage=(int) Math.ceil(studentCount/(double)count);//总页数
		//按条件信息查询出来的学生信息
		ArrayList<Users> student=aservice.selectStudentByCourse(cocourseCode, cid, pageStart, count);
		ArrayList<Course> courseName=acourse.selectCourseName();//选择数据库中的课程信息
		mav.addObject("student", student);
		mav.addObject("page",page);//当前页数
		mav.addObject("totoalPage",totoalPage);//总共页数
		mav.addObject("cocourseCode", cocourseCode);//当前课程代码
		mav.addObject("courseName", courseName);//课程信息
		mav.addObject("cid", cid);//当前班级名称
		mav.addObject("menu", "StudentInformation");//当前的menu名称
		mav.setViewName("/admin/Users/StudentInformation");//页面跳转
		return mav;
		
	}
	/**
	 * 通过选择课程来选择班级
	 * @param cocourseCode
	 * @return
	 */
	@ResponseBody
	@RequestMapping("selectClasses")
	public ArrayList<Classes> selectClasses(int cocourseCode){
		ArrayList<Classes> classes =acourse.selectClasses(cocourseCode);
		return classes;
	}
	/**
	 * 按照学生的id来查询学生信息
	 * @param uid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("selectStudentByUid")
	public Users selectStudentByUid(int uid){
		Users studentByUid=aservice.selectStudentByUid(uid);
		return  studentByUid;
	}
	/**
	 * 按照学生的id来修改学生信息
	 * @param uid
	 * @param uname
	 * @param unumber
	 * @param ugender
	 * @param ugrade
	 * @return
	 */
	@ResponseBody
	@RequestMapping("changeStudent")
	public int changeStudent (int uid,String uname,String unumber,String ugender,int ugrade){
		int result;
		Users changeStudent=new Users();
		changeStudent.setUid(uid);
		changeStudent.setUnumber(unumber);
		changeStudent.setUname(uname);
		changeStudent.setUgender(ugender);
		changeStudent.setUgrade(ugrade);
		result=aservice.changeStudent(changeStudent);
		return result;
	}
	/**
	 * 对学生成绩信息进行分页处理
	 * @param page
	 * @return
	 */
	@RequestMapping("studentScorePaging")
	public ModelAndView studentScorePaging(int page){
		ModelAndView mav=new ModelAndView();
		int count=5;//一页显示多少条
		int pageStart=(page-1)*5;//在数据库中查询的起点
		int studnetCount=aservice.studentScoreCount();//总共多少条
		int totoalPage=(int) Math.ceil(studnetCount/(double)count);//总共显示的页数
		ArrayList<Score> studentScore=aservice.studentScorePaging(pageStart, count);
		ArrayList<Course> courseName=acourse.selectCourseName();//选择数据库中的课程信息
		mav.addObject("courseName", courseName);//课程信息
		mav.addObject("studentScore", studentScore);//显示学生成绩信息
		mav.addObject("page", page);//当前页
		mav.addObject("totoalPage",totoalPage);//总共页数
		mav.addObject("menu", "StudentScore");//当前menu
		mav.setViewName("/admin/Users/StudentScore");
		return mav;
	}
	
	@RequestMapping("selectStudentScoreByCourse")
	public  ModelAndView selectStudentScoreByCourse(int cocourseCode,int cid,int page)
	{
		ModelAndView mav=new ModelAndView();
		int count=5;//每页显示的学生人数
		int pageStart=(page-1)*5;//在数据库中查询的起点
		int studentCount=aservice.selectScoreCountByCourse(cocourseCode, cid);//当前选择条件的总人数'
		System.out.println("==============================="+studentCount);
		int totoalPage=(int) Math.ceil(studentCount/(double)count);//总页数
		//按条件信息查询出来的学生信息
		ArrayList<Score> studentScore=aservice.selectStudentScoreByCourse(cocourseCode, cid, pageStart, count);
		ArrayList<Course> courseName=acourse.selectCourseName();//选择数据库中的课程信息
		mav.addObject("studentScore", studentScore);//显示学生成绩信息
		mav.addObject("page",page);//当前页数
		mav.addObject("totoalPage",totoalPage);//总共页数
		mav.addObject("cocourseCode", cocourseCode);//当前课程代码
		mav.addObject("courseName", courseName);//课程信息
		mav.addObject("cid", cid);//当前班级名称
		mav.addObject("menu", "StudentScore");//当前的menu名称
		mav.setViewName("/admin/Users/StudentScore");//页面跳转
		return mav;
		
	}
	
	
	/**
	 * 跳转insert页面
	 * @return
	 */
	@RequestMapping("/userImport")
	public ModelAndView userImport(){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("/admin/Users/insertStudents");
		return mav;
	};
	/**
	 * 显示所有学生信息
	 * @return
	 */

	@RequestMapping("/showStudents")
	public ModelAndView showStudents(){
		List<Users> student=new ArrayList<>(aservice.showAllStudent());
		ModelAndView mav=new ModelAndView();
		mav.addObject("student", student);
		mav.setViewName("/admin/Users/StudentInformation");
		return mav;
		
	}
	/**
	 * 导出学生成绩表
	 * @param response
	 */
	
	@RequestMapping("/studentlist.xlsx")
	public void test(HttpServletResponse response){
		XSSFWorkbook wb=aservice.student_score();
		try {
			wb.write(response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/**
	 *导入学生信息表
	 */
	@ResponseBody
	@RequestMapping("/insertStudents")
	public void insertStudents(MultipartFile excel){
		List<ImportExcelReprotModel<Users>> repot=aservice.insertStudents(excel);

	}
	/**
	 * 套题信息管理
	 * @param page
	 * @return
	 */
	@RequestMapping("ExamsInformation")
	public ModelAndView ExamsInformation(int page){
		ModelAndView mav=new ModelAndView();
		return mav;
		
	}
}
